home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 August: Tool Chest / Dev.CD Aug 00 TC Disk 2.toast / pc / sample code / interapplication comm / moreosl / moreappearance / moreappearance.cp next >
Encoding:
Text File  |  2000-06-23  |  9.9 KB  |  450 lines

  1. /*
  2.     File:        MoreAppearance.cp
  3.  
  4.     Contains:    
  5.  
  6.     Written by:    Pete Gontier
  7.  
  8.     Copyright:    Copyright (c) 1998 Apple Computer, Inc., All Rights Reserved.
  9.  
  10.                 You may incorporate this Apple sample source code into your program(s) without
  11.                 restriction. This Apple sample source code has been provided "AS IS" and the
  12.                 responsibility for its operation is yours. You are not permitted to redistribute
  13.                 this Apple sample source code as "Apple sample source code" after having made
  14.                 changes. If you're going to re-distribute the source, we require that you make
  15.                 it clear in the source that the code was descended from Apple sample source
  16.                 code, but that you've made changes.
  17.  
  18.     Change History (most recent first):
  19.  
  20.          <4>     20/3/00    Quinn   The various "have this feature" routines now auto-initialise. 
  21.                                     Made this change so that other MIB modules can call them without
  22.                                     worrying about initialising this module.
  23.          <3>     1/22/99    PCG     TARGET_CARBON
  24.          <2>    11/11/98    PCG     fix header
  25.          <1>    11/10/98    PCG     first big re-org at behest of Quinn
  26.  
  27.     Old Change History (most recent first):
  28.  
  29.          <6>    10/11/98    Quinn   Convert "MorePrefix.h" to "MoreSetup.h".
  30.          <5>     9/11/98    PCG     conditionalize calls to Appearance Manager 1.1 so they occur on
  31.                                     PowerPC only and add sanity checks for Appearance Manager
  32.                                     availability
  33.          <4>      9/4/98    PCG     theme drawing state functions call real APIs if present
  34.          <3>      9/1/98    PCG     clean up Gestalt cache logic; MoreGetThemeDrawingState inits
  35.                                     stateResult to NIL
  36.          <2>      9/1/98    PCG     add theme drawing state functions
  37.          <1>     6/16/98    PCG     initial checkin
  38. */
  39.  
  40. #include "MoreSetup.h"
  41.  
  42. #include "MoreAppearance.h"
  43. #include "MoreQuickDraw.h"
  44.  
  45. #include <Appearance.h>
  46. #include <Gestalt.h>
  47. #include <MacMemory.h>
  48.  
  49. static Boolean gHaveInited;
  50. static UInt32  gAppearanceAttributes;
  51. static UInt32  gAppearanceVersion;
  52.  
  53. pascal long GetAppearanceVersion (void)
  54. {
  55.     OSStatus junk;
  56.     
  57.     //
  58.     //    Simply returns our cached variable. See InitMoreAppearance
  59.     //    for how this variable is initialized.
  60.     //
  61.  
  62.     if (!gHaveInited) {
  63.         junk = InitMoreAppearance();
  64.         MoreAssertQ(junk == noErr);
  65.     }
  66.     return gAppearanceVersion;
  67. }
  68.  
  69. pascal Boolean HaveAppearance (void)
  70. {
  71.     OSStatus junk;
  72.  
  73.     //
  74.     //    Tests a bit in our cached variable and promotes it to a byte
  75.     //    indicating whether the Appearance APIs are present.
  76.     //
  77.  
  78.     if (!gHaveInited) {
  79.         junk = InitMoreAppearance();
  80.         MoreAssertQ(junk == noErr);
  81.     }
  82.     return (gAppearanceAttributes & (1 << gestaltAppearanceExists)) != 0;
  83. }
  84.  
  85. pascal Boolean AppearanceInCompatibilityMode (void)
  86. {
  87.     OSStatus junk;
  88.  
  89.     //
  90.     //    Tests a bit in our cached variable and promotes it to a byte
  91.     //    indicating whether Appearance is running in compatibility mode.
  92.     //
  93.  
  94.     if (!gHaveInited) {
  95.         junk = InitMoreAppearance();
  96.         MoreAssertQ(junk == noErr);
  97.     }
  98.     return (gAppearanceAttributes & (1 << gestaltAppearanceCompatMode)) != 0;
  99. }
  100.  
  101. pascal OSStatus InitMoreAppearance (void)
  102. {
  103.     //
  104.     //    Caches some Gestalt values so we don't have to keep calling Gestalt.
  105.     //    If Appearance is present, registers us as an Appearance-savvy app.
  106.     //    Second-guesses the version advertised by Gestalt.
  107.     //
  108.  
  109.     gHaveInited = true;
  110.     
  111.     OSStatus err = Gestalt (gestaltAppearanceAttr, (SInt32 *) &gAppearanceAttributes);
  112.  
  113.     if (err == gestaltUndefSelectorErr)
  114.     {
  115.         gAppearanceAttributes = 0;
  116.         err = noErr;
  117.     }
  118.     else if (!err)
  119.     {
  120.         err = Gestalt (gestaltAppearanceVersion, (SInt32 *) &gAppearanceVersion);
  121.  
  122.         if (err == gestaltUndefSelectorErr)
  123.         {
  124.             gAppearanceVersion = 0x0100;
  125.             err = noErr;
  126.         }
  127.     }
  128.  
  129.     if (!err && HaveAppearance ( ))
  130.     {
  131.         //
  132.         //    Perform sanity checking for potential weak links.
  133.         //    Even though Gestalt claims a certain version of
  134.         //    Appearance is available, we still may not have
  135.         //    successfully resolved all weak links to it. So
  136.         //    we second-guess Gestalt's claim and fall back to
  137.         //    progressively less useful versions of Appearance.
  138.         //    This is not an extensible approach; Appearance
  139.         //    Manager 2.0 weak links might be unresolved and
  140.         //    this code would never know to check. Still, this
  141.         //    code is only for ADDITIONAL paranoia/safety, and
  142.         //    a new Appearance Manager will not render it
  143.         //    invalid, just a little less canonical.
  144.         //
  145.  
  146. #if TARGET_RT_MAC_CFM
  147.  
  148. #if TARGET_CPU_PPC
  149.  
  150.         if (gAppearanceVersion >= 0x0110 && !GetTheme)
  151.             gAppearanceVersion = 0x0101;
  152.  
  153. #endif // TARGET_CPU_PPC
  154.  
  155.         if (gAppearanceVersion >= 0x0101 && !DrawThemeModelessDialogFrame)
  156.             gAppearanceVersion = 0x0100;
  157.  
  158.         if (gAppearanceVersion >= 0x0100 && !RegisterAppearanceClient)
  159.         {
  160.             gAppearanceVersion        = 0;
  161.             gAppearanceAttributes    = 0;
  162.         }
  163.  
  164. #endif // TARGET_RT_MAC_CFM
  165.  
  166.         if (HaveAppearance ( ))
  167.             err = RegisterAppearanceClient ( );
  168.     }
  169.  
  170.     return err;
  171. }
  172.  
  173. #pragma mark -
  174.  
  175. #if !TARGET_CARBON
  176.  
  177. struct MoreThemeDrawingStateTag
  178. {
  179.     //
  180.     //    stolen from
  181.     //        {CommonSystem}:Toolbox:ToolboxUtils:CommonUtilities:ColorPenState.c
  182.     //
  183.  
  184.     Boolean            colorPort;
  185.     Boolean            bkPatternIsValid;
  186.     RGBColor        foreColor;
  187.     RGBColor        backColor;
  188.     PenState        pen;
  189.     SInt16            textMode;
  190.     PixPatHandle    pnPixPat;
  191.     PixPatHandle    bkPixPat;
  192.     Pattern            bkPat;
  193.     UInt32            fgColor;
  194.     UInt32            bkColor;
  195. };
  196.  
  197. #endif
  198.  
  199. pascal OSStatus MoreGetThemeDrawingState (MoreThemeDrawingState *stateResult)
  200. {
  201.     //
  202.     //    mostly stolen from
  203.     //        {CommonSystem}:Toolbox:ToolboxUtils:CommonUtilities:ColorPenState.c
  204.     //
  205.  
  206.     *stateResult = nil;
  207.  
  208.     OSStatus err = noErr;
  209.  
  210. #if TARGET_CPU_PPC
  211.  
  212.     if (::GetAppearanceVersion ( ) >= 0x0110 && ::GetThemeDrawingState)
  213.         err = ::GetThemeDrawingState ((ThemeDrawingState *) stateResult);
  214.     else
  215.  
  216. #endif
  217.  
  218. #if TARGET_CARBON
  219.         err = unimpErr;
  220. #else
  221.  
  222.     {
  223.         MoreThemeDrawingState state = MoreThemeDrawingState (::NewPtrClear (sizeof (*state)));
  224.  
  225.         if (!state)
  226.             err = ::MemError ( );
  227.         else
  228.         {
  229.             GrafPtr curPort;
  230.             
  231.             ::GetPort( &curPort );
  232.             
  233.             state->pnPixPat = nil;
  234.             state->bkPixPat = nil;
  235.  
  236.             state->colorPort = ::IsColorGrafPort( curPort );
  237.             
  238.             // Save the black and white information always
  239.             
  240.             state->bkPatternIsValid = true;
  241.             state->bkPat = curPort->bkPat;
  242.             state->bkColor = curPort->bkColor;
  243.             state->fgColor = curPort->fgColor;
  244.  
  245.             if ( state->colorPort )
  246.             {
  247.                 ::GetForeColor( &state->foreColor );
  248.                 ::GetBackColor( &state->backColor );
  249.                 
  250.                 //
  251.                 // If the pen pattern is not an old style pattern,
  252.                 // copy the handle. If it is an old style pattern,
  253.                 // GetPenState below will save the right thing.
  254.                 //
  255.  
  256.                 PixPatHandle    penPixPat = ((CGrafPtr)curPort)->pnPixPat;
  257.                 PixPatHandle    backPixPat = ((CGrafPtr)curPort)->bkPixPat;
  258.                 
  259.                 if ( penPixPat != NULL )
  260.                     if( (**penPixPat).patType != 0 )
  261.                         state->pnPixPat = penPixPat;
  262.  
  263.                 //
  264.                 // If the back pattern is not an old style pattern,
  265.                 // copy the handle, else get the old pattern into
  266.                 // bkPat for restoring that way.
  267.                 //
  268.                 
  269.                 if( backPixPat != NULL )
  270.                 {
  271.                     if ( (**backPixPat).patType != 0 )
  272.                         state->bkPixPat = backPixPat;
  273.                     else
  274.                         state->bkPat = *(PatPtr)(*(**backPixPat).patData);
  275.                     
  276.                     state->bkPatternIsValid = true;
  277.                 }
  278.                 else
  279.                 {
  280.                     state->bkPatternIsValid = false;
  281.                 }
  282.                 
  283.             }
  284.             
  285.             ::GetPenState( &state->pen );
  286.             state->textMode = curPort->txMode;
  287.  
  288.             *stateResult = state;
  289.         }
  290.     }
  291.  
  292. #endif
  293.  
  294.     return err;
  295. }
  296.  
  297. pascal OSStatus MoreNormalizeThemeDrawingState (void)
  298. {
  299.     //
  300.     //    mostly stolen from
  301.     //        {CommonSystem}:Toolbox:ToolboxUtils:CommonUtilities:ColorPenState.c
  302.     //
  303.  
  304.     OSStatus err = noErr;
  305.  
  306. #if TARGET_CPU_PPC
  307.  
  308.     if (::GetAppearanceVersion ( ) >= 0x0110 && ::NormalizeThemeDrawingState)
  309.         err = NormalizeThemeDrawingState ( );
  310.     else
  311.  
  312. #endif
  313.  
  314. #if TARGET_CARBON
  315.         err = unimpErr;
  316. #else
  317.  
  318.     {
  319.         static Pattern    whitePat = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
  320.         GrafPtr            curPort;
  321.         
  322.         ::GetPort( &curPort );
  323.         ::ForeColor( blackColor );
  324.         ::BackColor( whiteColor );
  325.         ::PenNormal ( );
  326.         ::BackPat( &whitePat );
  327.         ::TextMode( srcOr );
  328.     }
  329.  
  330. #endif
  331.  
  332.     return err;
  333. }
  334.  
  335. pascal OSStatus MoreSetThemeDrawingState (MoreThemeDrawingState state, Boolean disposeNow)
  336. {
  337.     //
  338.     //    mostly stolen from
  339.     //        {CommonSystem}:Toolbox:ToolboxUtils:CommonUtilities:ColorPenState.c
  340.     //    and
  341.     //        {CommonSystem}:Toolbox:Appearance:AppearanceCore:ThemeDrawingState.cp
  342.     //
  343.  
  344.     OSStatus err = noErr;
  345.  
  346.     if (!state)
  347.         err = paramErr;
  348.     else
  349.  
  350. #if TARGET_CPU_PPC
  351.  
  352.     if (::GetAppearanceVersion ( ) >= 0x0110 && ::SetThemeDrawingState)
  353.         err = ::SetThemeDrawingState (ThemeDrawingState (state), disposeNow);
  354.     else
  355.  
  356. #endif
  357.  
  358. #if TARGET_CARBON
  359.         err = unimpErr;
  360. #else
  361.  
  362.     {
  363.         GrafPtr curPort;
  364.         
  365.         ::GetPort( &curPort );
  366.  
  367.         ::SetPenState (&(state->pen));
  368.  
  369.         //
  370.         // If we saved color information, and this port is a
  371.         // color port, use the color stuff, else just use the
  372.         // black and white information.
  373.         //
  374.     
  375.         if ( ::IsColorGrafPort( curPort ) && state->colorPort )
  376.         {
  377.             ::RGBForeColor( &state->foreColor );
  378.             ::RGBBackColor( &state->backColor );
  379.  
  380.             if ( state->pnPixPat != NULL )
  381.                 ::PenPixPat( state->pnPixPat );
  382.             
  383.             if( state->bkPatternIsValid )
  384.             {
  385.                 if ( state->bkPixPat != NULL )
  386.                     ::BackPixPat( state->bkPixPat );
  387.                 else
  388.                     ::BackPat( &state->bkPat );
  389.             }
  390.         }
  391.         else
  392.         {
  393.             //
  394.             // back pattern is always valid for monochrome ports
  395.             //
  396.         
  397.             ::BackPat( &state->bkPat );
  398.             ::ForeColor( state->fgColor );
  399.             ::BackColor( state->bkColor );
  400.         }
  401.  
  402.         ::TextMode( state->textMode );
  403.  
  404.         if (disposeNow)
  405.         {
  406.             ::DisposePtr (Ptr (state));
  407.             err = ::MemError ( );
  408.         }
  409.     }
  410.  
  411. #endif
  412.  
  413.     return err;
  414. }
  415.  
  416. pascal OSStatus MoreDisposeThemeDrawingState (MoreThemeDrawingState state)
  417. {
  418.     //
  419.     //    mostly stolen from
  420.     //        {CommonSystem}:Toolbox:Appearance:AppearanceCore:ThemeDrawingState.cp
  421.     //
  422.  
  423.     OSStatus err = noErr;
  424.  
  425.     if (!state)
  426.         err = paramErr;
  427.     else
  428.  
  429. #if TARGET_CPU_PPC
  430.  
  431.     if (::GetAppearanceVersion ( ) >= 0x0110 && ::DisposeThemeDrawingState)
  432.         err = ::DisposeThemeDrawingState (ThemeDrawingState (state));
  433.     else
  434.  
  435. #endif
  436.  
  437. #if TARGET_CARBON
  438.         err = unimpErr;
  439. #else
  440.  
  441.     {
  442.         ::DisposePtr (Ptr (state));
  443.         err = ::MemError ( );
  444.     }
  445.  
  446. #endif
  447.  
  448.     return err;
  449. }
  450.